﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using LunwenYun.Infrastructure.Entities.Repository;
using LunwenYun.Infrastructure.Entities.Enum;

namespace LunwenYun.Infrastructure.Entities.Entity
{
    public class Trade : IAccessible<T_Trade, Trade>, ILogicEntity<T_Trade, Trade>
    {
        #region 
        public int Id { get; set; }
        public int ArticleId { get; set; }
        /// <summary>
        /// 交易金额
        /// </summary>
        public decimal Momey { get; set; }

        /// <summary>
        /// 交易描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 状态
        /// 1：发起交易 
        /// 2：确认交易，待支付 
        /// 3：已付款，交易中 
        /// 4：交易完成，待确认（约稿人发起） 
        /// 5：交易完成，待确认（投稿人发起）
        /// 6：交易已确认
        /// 7：交易完成
        /// 8：有争议
        /// 99：交易取消
        /// </summary>
        public TradeStatus Status { get; set; }
    
        /// <summary>
        /// 约稿人
        /// </summary>
        public string Supplier { get; set; }

        /// <summary>
        /// 投稿人
        /// </summary>
        public string Consumer { get; set; }

        /// <summary>
        /// 交易发起时间
        /// </summary>
        public DateTime OpenDate { get; set; }

        /// <summary>
        /// 交易内容修改时间
        /// </summary>
        public DateTime LastModify { get; set; }

        public string Token { get; set; }

        /// <summary>
        /// 交易待确认发起时间
        /// </summary>
        public DateTime ConfirmTradeDate { get; set; }

        /// <summary>
        /// 确认超时时间。到达这个时间过后。交易自动完成
        /// </summary>
        public DateTime ConfirmExpireDate { get; set; }

        /// <summary>
        /// 交易完成时间
        /// </summary>
        public DateTime TradingDate { get; set; }
        public int PeriodicalId { get; set; }
        public string OpenUsername { get; set; }
        public bool IsDelete { get; set; }
        public int Organization { get; set; }
        #endregion

        public string ConsumerName { get; set; }
        public string SupplierName { get; set; }
        public PaperAgentInfo AgentInfo { get; set; }
        public Article Article { get; set; }
        public Periodical Periodical { get; set; }
        public Organization IndustryOrganization { get; set; }

        protected override void Covariant(T_Trade model)
        {
            if (model != null)
            {
                this.ConfirmExpireDate = model.ConfirmExpireDate;
                this.ConfirmTradeDate = model.ConfirmTradeDate;
                this.Consumer = model.Consumer;
                this.Description = model.Description;
                this.Id = model.Id;
                this.ArticleId = model.ArticleId;
                this.LastModify = model.LastModify;
                this.Momey = model.Momey;
                this.OpenDate = model.OpenDate;
                this.Status = (TradeStatus)model.Status;
                this.Supplier = model.Supplier;
                this.Token = model.Token;
                this.TradingDate = model.TradingDate;
                this.PeriodicalId = model.PeriodicalId;
                this.OpenUsername = model.OpenUsername;
                this.IsDelete = model.IsDelete;
                this.Organization = model.Organization;
            }
        }

        protected override T_Trade Inverter(Trade model)
        {
            if (model != null)
            {
                return new T_Trade()
                {
                    ConfirmExpireDate = model.ConfirmExpireDate,
                    ConfirmTradeDate = model.ConfirmTradeDate,
                    Consumer = model.Consumer,
                    Description = model.Description,
                    Id = model.Id,
                    ArticleId = model.ArticleId,
                    LastModify = model.LastModify,
                    Momey = model.Momey,
                    OpenDate = model.OpenDate,
                    Status = (int)model.Status,
                    Supplier = model.Supplier,
                    Token = model.Token,
                    TradingDate = model.TradingDate,
                    PeriodicalId = model.PeriodicalId,
                    OpenUsername = model.OpenUsername,
                    IsDelete=model.IsDelete,
                    Organization = model.Organization
                };
            }
            return null;
        }

        public Trade Set(T_Trade dbModel)
        {
            Covariant(dbModel);
            return this;
        }
        public Trade LoadOranization(T_Organization t_organization)
        {
            this.IndustryOrganization = new Organization().Set(t_organization);
            return this;
        }

        public Trade LoadAgentInfo(T_PaperAgentInfo t_PaperAgentInfo)
        {
            this.AgentInfo = new PaperAgentInfo().Set(t_PaperAgentInfo);
            return this;
        }

        public Trade LoadArticle(T_Article t_article)
        {
            if (t_article != null)
            {
                this.Article = new Article().Set(t_article);
            }
            return this;
        }

        public Trade LoadPeriodical(T_Periodical t_Periodical)
        {
            if (t_Periodical != null)
            {
                this.Periodical = new Periodical().Set(t_Periodical);
            }
            return this;
        }
    }
}
